/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.model.app.AppMyInfoResultModel;
import com.jnzc.ocr.rest.app.shop.model.app.AppMyInfoSaveParamModel;
import com.jnzc.ocr.rest.app.shop.param.app.AppSystemSaveParam;
import com.jnzc.ocr.serv.shop.api.ShopMainApi;
import com.jnzc.ocr.serv.shop.api.ShopUserApi;
import com.jnzc.ocr.serv.shop.consts.ShopDictConsts;
import com.jnzc.ocr.serv.shop.entity.ShopMain;
import com.jnzc.ocr.serv.shop.entity.ShopUser;
import com.jnzc.ocr.serv.system.api.SysDictApi;
import com.jnzc.ocr.serv.system.vo.DictPair;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/11
 */
@Api(tags = {"app - 我的信息接口"})
@RestController
@RequestMapping("/app/myInfo")
public class AppMyInfoController extends BaseController {

    @Autowired
    private ShopMainApi shopMainApi;

    @Autowired
    private ShopUserApi shopUserApi;

    @Autowired
    private SysDictApi sysDictApi;

    @ApiOperation(value = "我的信息")
    @GetMapping("")
    public AppMyInfoResultModel index(){
        UserVo userVo = super.get();
        // 返回结果
        AppMyInfoResultModel result = new AppMyInfoResultModel();
        ShopMain shopMain = shopMainApi.find(userVo.getShopId());
        result.setShopMain(shopMain);
        ShopUser shopUser = shopUserApi.find(userVo.getId());
        result.setShopUser(shopUser);
        return result;
    }

    @ApiOperation(value = "保存我的信息")
    @PostMapping("/save")
    public void save(@RequestBody AppMyInfoSaveParamModel param){
        ValidateUtils.notEmpty(param.getShopName(), 1, 20, "店铺名称");
        ValidateUtils.notEmpty(param.getName(), 1, 20, "姓名");
        ValidateUtils.isMobile(param.getMobile());
        UserVo userVo = super.get();
        // 修改店铺信息
        ShopMain shopMain = shopMainApi.find(userVo.getShopId());
        shopMain.setName(param.getShopName());
        shopMain.setCategoryId(param.getCategoryId());
        if(null != param.getCategoryId()){
            DictPair dictPair = sysDictApi.findPair(ShopDictConsts.SHOP_CATEGORY, param.getCategoryId().intValue());
            shopMain.setCategoryName(dictPair.getName());
        }
        shopMain.setProvinceCode(param.getProvinceCode());
        shopMain.setProvinceName(param.getProvinceName());
        shopMain.setCityCode(param.getCityCode());
        shopMain.setCityName(param.getCityName());
        shopMain.setAddress(param.getAddress());
        shopMainApi.update(shopMain);
        // 修改店铺用户信息
        ShopUser shopUser = shopUserApi.find(userVo.getId());
        shopUser.setName(param.getName());
        shopUser.setMobile(param.getMobile());
        shopUserApi.update(shopUser);
    }

    @ApiOperation(value = "系统设置")
    @PostMapping("/saveSystem")
    public void saveSystem(@RequestBody AppSystemSaveParam param){
        UserVo userVo = super.get();
        ShopMain shopMain = shopMainApi.find(userVo.getShopId());
        shopMain.setWarnStockNum(param.getWarnStockNum());
        shopMain.setWarnCheckDay(param.getWarnCheckDay());
        shopMain.setWeixinpayImage(param.getWeixinpayImage());
        shopMain.setAlipayImage(param.getAlipayImage());
        shopMainApi.update(shopMain);
    }
}
